<?php

namespace app\stores\logic\printer;

use app\beautiful\model\appointment\AppointmentModel;

use app\beautiful\model\logs\ProjectWriteOffModel;

use app\beautiful\model\logs\StoreWriteOffModel;

use app\beautiful\model\member\MemberModel;

use app\beautiful\model\member\MemberOnceCardItemModel;

use app\beautiful\model\member\MemberOnceCardModel;

use app\beautiful\model\member\MemberSnapUpModel;

use app\beautiful\model\order\BuyOrderModel;

use app\beautiful\model\order\CashierOrderItemModel;

use app\beautiful\model\order\CashierOrderModel;

use app\beautiful\model\order\GoodsOrderItemModel;

use app\beautiful\model\order\GoodsOrderModel;

use app\beautiful\model\order\StoreOrderModel;



class PrinterFeieContentLogic{

    protected $order;

    protected $member;

    protected $order_item;

    protected $record;

    protected $once_card;



    public  function buy($shop,$id)

    {

        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>买单</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.='订单总金额 {balance}<BR>';

        $content.='会员优惠金额 {vip_balance}<BR>';

        $content.='优惠券优惠金额 {coupon_money}<BR>';

        $content.='积分抵用的金额 {integral_balance}<BR>';

        $content.="--------------------------------<BR>";

        $content.='支付方式 {pay_type_mean}<BR>';

        $content.="支付时间 {pay_time_format}<BR>";

        $content.="实收 {price}元<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $this->checkOrder($id);

        $this->checkMember($this->order->member_id);



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->order->buy_order_id,$content);

        $content=str_replace('{add_time_format}',$this->order->add_time_format,$content);

        $content=str_replace('{balance}',$this->order->balance,$content);

        $content=str_replace('{vip_balance}',$this->order->vip_balance,$content);

        $content=str_replace('{coupon_money}',$this->order->coupon_money,$content);

        $content=str_replace('{integral_balance}',$this->order->integral_balance,$content);

        $content=str_replace('{pay_type_mean}',$this->order->pay_type_mean,$content);

        $content=str_replace('{pay_time_format}',$this->order->pay_time_format,$content);

        $content=str_replace('{price}',$this->order->price,$content);



        return $content;

    }



    public function orderWriteOff($shop,$id)

    {

        $this->checkGoodsOrder($id);

        $this->checkMember($this->order->member_id);

        $this->chechGoodsOrderItem($id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>订单核销</BOLD></C>";

        $content.="<C><BOLD>{order_type_means}</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.='订单总金额 {total_price}<BR>';

        $content.='优惠券优惠金额 {coupon_money}<BR>';

        $content.='积分抵用的金额 {integral_balance}<BR>';

        $content.='优惠金额 {youhui_balance}<BR>';

        $content.='实付金额 {need_pay}<BR>';

        $content.="--------------------------------<BR>";

        $content.="--------------------------------<BR>";

        foreach ($this->order_item as $item){

            $content.=$item['goods_name']." *".$item['goods_num']."<BR>";

        }

        $content.="--------------------------------<BR>";

        $content.='支付方式 {pay_type_mean}<BR>';

        $content.="支付时间 {pay_time_format}<BR>";

        $content.="核销时间 {write_off_time_format}<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{order_type_means}',$this->order->order_type_means,$content);

        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->order->order_id,$content);

        $content=str_replace('{add_time_format}',$this->order->add_time_format,$content);

        $content=str_replace('{total_price}',$this->order->total_price,$content);

        $content=str_replace('{coupon_money}',$this->order->coupon_money,$content);

        $content=str_replace('{integral_balance}',$this->order->integral_balance,$content);

        $content=str_replace('{youhui_balance}',$this->order->youhui_balance,$content);

        $content=str_replace('{need_pay}',$this->order->need_pay,$content);

        $content=str_replace('{pay_type_mean}',$this->order->pay_type_mean,$content);

        $content=str_replace('{pay_time_format}',$this->order->pay_time_format,$content);

        $content=str_replace('{write_off_time_format}',date("Y-m-d H:i:s",time()),$content);



        return $content;

    }



    public function storeOrderWriteOff($shop,$id)

    {

        $this->checkStoreGoodsRecord($id);

        $this->checkStoreGoodsOrder($this->record->store_order_id);

        $this->checkMember($this->order->member_id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>囤货订单核销</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.='订单总金额 {goods_total_price}<BR>';

        $content.='优惠金额 {youhui_balance}<BR>';

        $content.='实付金额 {need_pay}<BR>';

        $content.="--------------------------------<BR>";

        $content.="--------------------------------<BR>";

        $content.='商品名称 {goods_name}<BR>';

        $content.='囤货数量 {goods_num}<BR>';

        $content.="--------------------------------<BR>";

        $content.='核销件数 {num}<BR>';

        $content.="剩余件数 {remain_num}<BR>";

        $content.="备注 {remark}<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->order->store_order_id,$content);

        $content=str_replace('{add_time_format}',$this->order->add_time_format,$content);

        $content=str_replace('{goods_total_price}',$this->order->goods_total_price,$content);

        $content=str_replace('{youhui_balance}',$this->order->youhui_balance,$content);

        $content=str_replace('{need_pay}',$this->order->need_pay,$content);

        $content=str_replace('{goods_name}',$this->order->goods_name,$content);

        $content=str_replace('{goods_num}',$this->order->goods_num,$content);

        $content=str_replace('{num}',$this->record->num,$content);

        $content=str_replace('{remain_num}',$this->record->remain_num,$content);

        $content=str_replace('{remark}',$this->record->remark,$content);





        return $content;

    }



    public function snapUpOrderWriteOff($shop,$id)

    {

        $this->checkProjectRecord($id);

        $this->checkMemberSnapUp($this->record->service_id);

        $this->checkMember($this->order->member_id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>抢购订单核销</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.='项目名称 {abbreviation}<BR>';

        $content.="--------------------------------<BR>";

        $content.='核销件数 {num}<BR>';

        $content.="剩余件数 {remain_num}<BR>";

        $content.="备注 {remark}<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->record->write_off_id,$content);

        $content=str_replace('{add_time_format}',$this->record->add_time_format,$content);

        $content=str_replace('{abbreviation}',$this->order->abbreviation,$content);

        $content=str_replace('{num}',$this->record->num,$content);

        $content=str_replace('{remain_num}',$this->record->remain_num,$content);

        $content=str_replace('{remark}',$this->record->remark,$content);





        return $content;

    }



    public function onceCardOrderWriteOff($shop,$id)

    {

        $this->checkProjectRecord($id);

        $this->checkMemberOnceCardItem($this->record->service_id);

        $this->checkMemberOnceCard($this->record->member_once_card_id);

        $this->checkMember($this->order->member_id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>次卡订单核销</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.="所属次卡 {once_card_name}<BR>";

        $content.='项目名称 {abbreviation}<BR>';

        $content.="--------------------------------<BR>";

        $content.='核销件数 {num}<BR>';

        $content.="剩余件数 {remain_num}<BR>";

        $content.="备注 {remark}<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->record->write_off_id,$content);

        $content=str_replace('{add_time_format}',$this->record->add_time_format,$content);

        $content=str_replace('{once_card_name}',$this->once_card->name,$content);

        $content=str_replace('{abbreviation}',$this->order->abbreviation,$content);

        $content=str_replace('{num}',$this->record->num,$content);

        $content=str_replace('{remain_num}',$this->record->remain_num,$content);

        $content=str_replace('{remark}',$this->record->remark,$content);





        return $content;

    }



    public function appointment($shop,$id)

    {

        $this->checkAppointment($id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>预约订单</BOLD></C>";

        $content.="姓名 {nick_name}<BR>";

        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.="--------------------------------<BR>";

        $content.="项目名称 {project_name}<BR>";

        $content.="--------------------------------<BR>";

        if($this->order->technician){

            $content.="--------------------------------<BR>";

            $content.="技师名称 {technician_name}<BR>";

            $content.="技师手机号 {technician_mobile}<BR>";

            $content.="--------------------------------<BR>";

        }



        $content.='预约日期 {day}<BR>';

        $content.="预约时间段 {time}<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";



        $content=str_replace('{shop_name}',$shop->name,$content);

        $content=str_replace('{nick_name}',$this->order->member_name,$content);

        $content=str_replace('{number}',$this->order->appointment_id,$content);

        $content=str_replace('{add_time_format}',$this->order->add_time_format,$content);

        $content=str_replace('{project_name}',$this->order->project_name,$content);

        $content=str_replace('{technician_name}',$this->order->technician_name,$content);

        $content=str_replace('{technician_mobile}',$this->order->technician_mobile,$content);

        $content=str_replace('{day}',$this->order->day,$content);

        $content=str_replace('{time}',$this->order->start_time.'-'.$this->order->end_time,$content);





        return $content;

    }





    public function cashOrder($shop,$id)

    {

        $this->checkCashOrder($id);

        $this->checkCashOrderItem($id);





        $content="";

        $content.="<CB>{shop_name}</CB><BR><BR>";

        $content.="<C><BOLD>收银商品订单</BOLD></C>";



        if($this->order->member_id==0){

            $content.="姓名 游客<BR>";

        }else{

            $this->checkMember($this->order->member_id);

            $content.="姓名 ".$this->member->nick_name."<BR>";

        }



        $content.="单号 {number}<BR>";

        $content.="时间 {add_time_format}<BR>";

        $content.=$this->getArrContent($this->order_item,21,3,6);



        $content.="--------------------------------<BR>";

        $content.='订单总金额 {balance}<BR>';

        $content.='优惠券抵扣金额 {coupon_money}<BR>';

        $content.='优惠金额 {youhui_price}<BR>';

        $content.="--------------------------------<BR>";

        $content.='支付方式 {pay_type_mean}<BR>';

        $content.="支付时间 {pay_time_format}<BR>";

        $content.="实收 {price}元<BR><BR>";

        $content.='签名<BR><BR><BR>';

        $content.="<C>欢迎再次光临</C>";







        $content=str_replace('{shop_name}',$shop->name,$content);

//        $content=str_replace('{nick_name}',$this->member->nick_name,$content);

        $content=str_replace('{number}',$this->order->order_id,$content);

        $content=str_replace('{add_time_format}',$this->order->add_time_format,$content);

        $content=str_replace('{balance}',$this->order->total_price,$content);

        $content=str_replace('{coupon_money}',$this->order->coupon_money,$content);

        $content=str_replace('{youhui_price}',$this->order->youhui_price,$content);

        $content=str_replace('{pay_type_mean}',$this->order->pay_type_mean,$content);

        $content=str_replace('{pay_time_format}',$this->order->pay_time_format,$content);

        $content=str_replace('{price}',$this->order->need_pay,$content);



        return $content;

    }



    protected function checkOrder($id)

    {

        if(null === $order = BuyOrderModel::find($id)) $this->error('订单不存在');

        $this->order=$order;

    }



    protected function checkGoodsOrder($id){

        if(null === $order = GoodsOrderModel::find($id)) $this->error('订单不存在');

        $this->order=$order;

    }



    protected function chechGoodsOrderItem($id)

    {

        $order_item = GoodsOrderItemModel::where('order_id',$id)->select()->toArray();

        $this->order_item=$order_item;

    }



    protected function checkStoreGoodsRecord($id)

    {

        if(null === $this->record = StoreWriteOffModel::find($id)) $this->error('记录不存在');

    }



    protected function checkStoreGoodsOrder($id)

    {

        if(null === $this->order = StoreOrderModel::find($id)) $this->error('订单不存在');

    }



    protected function checkMember($id)

    {

        if(null === $member = MemberModel::find($id))  $this->error('找不到会员');

        $this->member=$member;

    }



    protected function checkProjectRecord($id)

    {

        if(null === $this->record = ProjectWriteOffModel::find($id))  $this->error('记录不存在');

    }



    protected function checkMemberSnapUp($id)

    {

        if(null === $this->order = MemberSnapUpModel::find($id)) $this->error('订单不存在');

    }



    protected function checkMemberOnceCardItem($id)

    {

        if(null === $this->order = MemberOnceCardItemModel::find($id)) $this->error('订单不存在');

    }



    protected function checkMemberOnceCard($id)

    {

        if(null === $this->once_card = MemberOnceCardModel::find($id)) $this->error('订单不存在');

    }



    public function checkAppointment($id)

    {

        if(null === $this->order = AppointmentModel::find($id)) $this->error('预约不存在');

    }



    public function checkCashOrder($id)

    {

        if(null === $this->order=CashierOrderModel::find($id)) $this->error('订单不存在');

    }



    public function checkCashOrderItem($id)

    {

        $order_item = CashierOrderItemModel::where('order_id',$id)->select()->toArray();

        $this->order_item=$order_item;

    }



    public function getArrContent($arr,$A,$C,$D)

    {

        $orderInfo = '';

        $orderInfo .= "产品                 数量 金额<BR>";

        $orderInfo .= "--------------------------------<BR>";

        foreach ($arr as $k5 => $v5) {

            $name = $v5['goods_name'];

            if($v5['goods_sku_id']>0){

                $name = $v5['goods_name'].'('.$v5['goods_sku_name'].')';

            }

            //$price = $v5['goods_sku_name'];

            $num = $v5['goods_num'];

            $prices = $v5['goods_price'];

            $kw3 = '';

            $kw1 = '';

            $kw2 = '';

            $kw4 = '';

            $str = $name;

            $blankNum = $A;//名称控制为14个字节

            $lan = mb_strlen($str,'utf-8');

            $m = 0;

            $j=1;

            $blankNum++;

            $result = array();

//            if(strlen($price) < $B){

//                $k1 = $B - strlen($price);

//                for($q=0;$q<$k1;$q++){

//                    $kw1 .= ' ';

//                }

//                $price = $price.$kw1;

//            }

            if(strlen($num) < $C){

                $k2 = $C - strlen($num);

                for($q=0;$q<$k2;$q++){

                    $kw2 .= ' ';

                }

                $num = $num.$kw2;

            }

            if(strlen($prices) < $D){

                $k3 = $D - strlen($prices);

                for($q=0;$q<$k3;$q++){

                    $kw4 .= ' ';

                }

                $prices = $prices.$kw4;

            }

            for ($i=0;$i<$lan;$i++){

                $new = mb_substr($str,$m,$j,'utf-8');

                $j++;

                if(mb_strwidth($new,'utf-8')<$blankNum) {

                    if($m+$j>$lan) {

                        $m = $m+$j;

                        $tail = $new;

                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);

                        $k = $A - strlen($lenght);

                        for($q=0;$q<$k;$q++){

                            $kw3 .= ' ';

                        }

                        if($m==$j){

                            $tail .= $kw3.' '.$num.' '.$prices;

                        }else{

                            $tail .= $kw3."<BR>";

                        }

                        break;

                    }else{

                        $next_new = mb_substr($str,$m,$j,'utf-8');

                        if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;

                        else{

                            $m = $i+1;

                            $result[] = $new;

                            $j=1;

                        }

                    }

                }

            }

            $head = '';

            foreach ($result as $key=>$value) {

                if($key < 1){

                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);

                    $v_lenght = strlen($v_lenght);

                    if($v_lenght == 13) $value = $value." ";

                    $head .= $value.' '.$num.' '.$prices;

                }else{

                    $head .= $value."<BR>";

                }

            }

            $orderInfo .= $head.$tail."<BR>";

            @$nums += $prices;

        }

        return $orderInfo;

    }



    public function error($message){

        throw new \Exception($message);

    }

}

